package edu.neu.agv_backend.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.neu.agv_backend.dto.AgvTaskDto;
import edu.neu.agv_backend.dto.AjaxResult;
import edu.neu.agv_backend.dto.TableDataInfo;
import edu.neu.agv_backend.entity.AgvTask;
import edu.neu.agv_backend.mapper.AgvTaskMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class TaskFromDBService {

    private final AgvTaskMapper taskMapper;

    public TaskFromDBService(AgvTaskMapper taskMapper) {
        this.taskMapper = taskMapper;
    }

    /**
     * 分页查询任务列表
     */
    public TableDataInfo<AgvTaskDto> listTask(int pageNum, int pageSize) {
        Page<AgvTask> page = new Page<>(pageNum, pageSize);
        taskMapper.selectPage(page, null);
        List<AgvTaskDto> rows = page.getRecords().stream().map(this::toDto).collect(Collectors.toList());

        TableDataInfo<AgvTaskDto> info = new TableDataInfo<>();
        info.setTotal(page.getTotal());
        info.setRows(rows);
        info.setCode(200);
        info.setMsg("success");
        return info;
    }

    public AjaxResult<AgvTaskDto> getTask(Long id) {
        AgvTask task = taskMapper.selectById(id);
        AjaxResult<AgvTaskDto> res = new AjaxResult<>();
        if (task != null) {
            res.setCode(200);
            res.setMsg("success");
            res.setData(toDto(task));
        } else {
            res.setCode(404);
            res.setMsg("Task not found");
        }
        return res;
    }

    public AjaxResult<Void> addTask(AgvTaskDto dto) {
        AgvTask entity = toEntity(dto);
        int rows = taskMapper.insert(entity);
        AjaxResult<Void> res = new AjaxResult<>();
        res.setCode(rows > 0 ? 200 : 500);
        res.setMsg(rows > 0 ? "success" : "insert failed");
        return res;
    }

    public AjaxResult<Void> updateTask(AgvTaskDto dto) {
        AgvTask entity = toEntity(dto);
        int rows = taskMapper.updateById(entity);
        AjaxResult<Void> res = new AjaxResult<>();
        res.setCode(rows > 0 ? 200 : 500);
        res.setMsg(rows > 0 ? "success" : "update failed");
        return res;
    }

    public AjaxResult<Void> delTask(Long id) {
        int rows = taskMapper.deleteById(id);
        AjaxResult<Void> res = new AjaxResult<>();
        res.setCode(rows > 0 ? 200 : 404);
        res.setMsg(rows > 0 ? "success" : "delete failed");
        return res;
    }

    /**
     * 启动任务，简单将状态改为"巡视中"
     */
    public AjaxResult<Void> startTask(Long id) {
        AgvTask task = taskMapper.selectById(id);
        AjaxResult<Void> res = new AjaxResult<>();
        if (task == null) {
            res.setCode(404);
            res.setMsg("Task not found");
            return res;
        }
        task.setTaskStatus("巡视中");
        taskMapper.updateById(task);
        res.setCode(200);
        res.setMsg("success");
        return res;
    }

    /**
     * 结束任务：根据 isAbort 标识设置任务状态
     */
    public AjaxResult<Void> endTask(Long id, boolean isAbort) {
        AgvTask task = taskMapper.selectById(id);
        AjaxResult<Void> res = new AjaxResult<>();
        if (task == null) {
            res.setCode(404);
            res.setMsg("Task not found");
            return res;
        }
        task.setTaskStatus(isAbort ? "已完成(中止)" : "已完成");
        taskMapper.updateById(task);
        res.setCode(200);
        res.setMsg("success");
        return res;
    }

    // 以下两个接口仅返回成功标识，实际业务按需实现
    public AjaxResult<Void> preUpload(Long id) {
        AjaxResult<Void> res = new AjaxResult<>();
        res.setCode(200);
        res.setMsg("success");
        return res;
    }

    public AjaxResult<Void> upload(Long id) {
        AjaxResult<Void> res = new AjaxResult<>();
        res.setCode(200);
        res.setMsg("success");
        return res;
    }

    private AgvTaskDto toDto(AgvTask entity) {
        AgvTaskDto dto = new AgvTaskDto();
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }

    private AgvTask toEntity(AgvTaskDto dto) {
        AgvTask entity = new AgvTask();
        BeanUtils.copyProperties(dto, entity);
        return entity;
    }
} 